package com.redteam.servlets;

import com.redteam.data.beans.Airplane;
import com.redteam.data.beans.Reservation;
import com.redteam.models.DAOAccessor;
import com.redteam.models.ReservationUpdater;
import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Takes the selected item and either checks them out or in
 * @author Anthony Ferrari
 */
public class CheckOutIn extends HttpServlet {
    /**
     * Default constructor for CheckOutIn
     */
    public CheckOutIn() {
    }

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String button = request.getParameter("button");
        DAOAccessor daoa = new DAOAccessor();
        long reservationID = Integer.parseInt(request.getParameter("reservationID"));
        Reservation reservation = daoa.getReservationById(reservationID);
        daoa = new DAOAccessor();
        Airplane plane = new Airplane();
        try {
            if (reservation.getDiscriminator().equals("airplane")) {
                plane = (Airplane) daoa.getPlane(reservation.getRentalItem().getId());
            }
        } catch (SQLException ex) {
            Logger.getLogger(CheckOutIn.class.getName()).log(Level.SEVERE, null, ex);
        }
        ServletContext context = getServletContext();
        ReservationUpdater ru = new ReservationUpdater();

        if (button.equals("checkin")) {
            reservation.setReservationState("finished");
            ru.updateReservation(reservation);
            if (reservation.getDiscriminator().equals("airplane")) {
                request.setAttribute("plane", plane);
                RequestDispatcher dispatch = context.getRequestDispatcher("/jsp/checkin.jsp");
                dispatch.forward(request, response);
            }
            RequestDispatcher dispatch = context.getRequestDispatcher("/UserHome");
            dispatch.forward(request, response);
        } else {
            reservation.setReservationState("inProgress");
            ru.updateReservation(reservation);
            RequestDispatcher dispatch = context.getRequestDispatcher("/jsp/checkout.jsp");
            dispatch.forward(request, response);
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
